package com.xawl.controller;

import com.github.pagehelper.PageHelper;
import com.xawl.dao.QuestionMapper;
import com.xawl.pojo.Question;
import com.xawl.service.QuestionService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    @Qualifier("questionServiceImpl")
    protected QuestionService questionService;

    @Autowired
    protected QuestionMapper questionMapper;

    // 查询问题  模糊查询
    @RequestMapping("/searchQuestion")
    public String searchQuestion(Model model, String title, HttpServletRequest req) {
        if (title == null || title == "") {
            model.addAttribute("error", "未查到");
        }
        List<Question> queryList = questionService.searchCourse(title);
        req.getSession().setAttribute("queryList", queryList);
        return "red-page/queryResult";
    }


    /* 获取数据列表 */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public JSONObject getList(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Question> questions = questionService.queryAll();
        Integer total = questionService.getTotal();
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", 1);
        json.put("count", total);
        json.put("data", questions);
        return json;
    }

    /* 增加 */
    @RequestMapping("/add")
    public String add() {
        return "red-question-query/red-question-query-edit";
    }

    @RequestMapping("/addData")
    @ResponseBody
    public String addQuestion(@RequestBody Question question) {
        int i = questionService.addQuestion(question);
        return i != 0 ? "ok" : "error";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") int id) {
        model.addAttribute("qId", id);
        return "red-question-query/red-question-query-edit";
    }

    /* 删除 */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestBody int id) {
        questionService.deleteQuestion(id);
        return "ok";
    }

    /* 删除多条记录 */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deletes(@RequestBody Long[] ids) {
        int i = questionService.deleteQuestions(ids);
        return i != 0 ? "ok" : "error";
    }

    /* 更新 */
    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody Question q) {
        int i = questionService.updateQuestion(q);
        return i != 0 ? "ok" : "error";
    }

    /* 查看 */
    @RequestMapping("/view/{id}")
    public String view(Model model, @PathVariable("id") int id) {
        model.addAttribute("qId", id);
        return "red-question-query/red-question-query-view";
    }

    /* 数据回显 */
    @RequestMapping("/viewData")
    @ResponseBody
    public Question viewData(@RequestBody int id) {
        return questionService.findQuestionById(id);
    }

}
